
	use "$_rawdata/rawdata/LSMS Malawi/2016/household/hh_mod_d.dta", clear
	compress
	tempfile hh_mod_d
	save `hh_mod_d', replace

	use "$_rawdata/rawdata/LSMS Malawi/2016/household/hh_mod_e.dta", clear
	compress
	tempfile hh_mod_e
	save `hh_mod_e', replace
	
	use "$_rawdata/rawdata/LSMS Malawi/2016/household/hh_mod_t.dta", clear
	compress
	tempfile hh_mod_t
	save `hh_mod_t', replace

	use "$_rawdata/rawdata/LSMS Malawi/2016/household/hh_mod_b.dta", clear // household roster
	compress
	
	/*
		5 MODULE B: HOUSEHOLD ROSTER 
		8 MODULE C: EDUCATION
		11 MODULE D: HEALTH
		16 MODULE E: TIME USE & LABOUR 
		32 MODULE F: HOUSING
		35 MODULE F_1: LAND ROSTER
		47 MODULE H: FOOD SECURITY
		48 MODULE I: NON-FOOD EXPENDITURES – OVER PAST ONE WEEK & ONE MONTH
		49 MODULE J: NON-FOOD EXPENDITURES
		50 MODULE K: NON-FOOD EXPENDITURES OVER PAST 12 MONTHS
		51 MODULE L: DURABLE GOODS
		53 MODULE M: FARM IMPLEMENTS, MACHINERY, AND STRUCTURES
		55 MODULE N: HOUSEHOLD ENTERPRISES
		63 MODULE O: CHILDREN LIVING ELSEWHERE
		65 MODULE P: OTHER INCOME
		67 MODULE Q: GIFTS GIVEN OUT
		68 MODULE R: SOCIAL SAFETY NETS
		70 MODULE S: CREDIT
		72 MODULE T: SUBJECTIVE ASSESSMENT OF WELL-BEING 
		74 MODULE U: SHOCKS & COPING STRATEGIES
		75 MODULE V: CHILD ANTHROPOMETRY
		76 MODULE W: DEATHS
		77 MODULE X: FILTER QUESTIONS FOR AGRICULTURE &
		FISHERY QUESTIONNAIRES
	*/
	
	merge m:1 case_id using "$_rawdata/rawdata/LSMS Malawi/2016/consumption_aggregate/ihs4 consumption aggregate.dta", assert(matched) nogen	// consumption aggregate
	merge 1:1 case_id pid using `hh_mod_d', keep(master matched) nogen // health
	merge 1:1 case_id pid using `hh_mod_e', keep(master matched) nogen // time use and labor
	merge m:1 case_id using `hh_mod_t', keep(master matched) nogen // subjective well-being
	merge m:1 case_id using "$_rawdata/rawdata/LSMS Malawi/2016/household/hh_mod_a_filt.dta", keep(master matched) nogen // for urban status
	
	ren hhid hhid_orig
	gen hhid=case_id
	ren pid indiv
	
	 ** INFORMATION FROM HOUSEHOLD ROSTER

		** Still in HH
			gen w4_inhh=1 // FIXME: No specific question asking this
		
		** Age
			gen w4_age=hh_b05a
			
		** Household size
			gen temp=1 if w4_inhh==1
			bys hhid: egen w4_hh_size=total(temp)
			drop temp

		** Number of children 12 or younger
			gen temp=1 if w4_age<=12 & w4_inhh==1
			bys hhid: egen w4_hh_under12=total(temp)
			drop temp

		** Number of youth 18 or younger
			gen temp=1 if w4_age<=18 & w4_inhh==1
			bys hhid: egen w4_hh_under18=total(temp)
			drop temp

		** Number of adults 60 or older
			gen temp=1 if w4_age>=60 & !missing(w4_age) & w4_age!=999 & w4_inhh==1
			bys hhid: egen w4_hh_over60=total(temp)
			drop temp

		** Adults 12 to 60
			gen temp=1 if w4_age>12 & w4_age<60 & !missing(w4_age) & w4_age!=999 & w4_inhh==1
			bys hhid: egen w4_hh_12to60=total(temp)
			drop temp

		** Marital status
			gen w4_married=(hh_b24==1 | hh_b24==2) if !missing(hh_b24)
			gen w4_married_monog=(hh_b24==1) if !missing(hh_b24)
			gen w4_married_polyg=(hh_b24==2) if !missing(hh_b24)

		** Female
			gen w4_female=(hh_b03==2) if !missing(hh_b03)

		** Age of spouse
			sum indiv
			forvalues x=1/`r(max)' {
				gen age_person`x'_temp=w4_age if indiv==`x' & !missing(w4_age)
				bys hhid: egen age_person`x'=max(age_person`x'_temp)
				drop age_person`x'_temp
			}

		** Average age of others in the household
			sum indiv 
			forvalues x=1/`r(max)' {
				replace age_person`x'=. if indiv==`x'
			}
			egen w4_hh_avgage=rowmean(age_person*)
			
		** Spouse age
			sum indiv
			forvalues x=1/`r(max)' {
				replace age_person`x'=. if hh_b26a!=`x'
			}
			egen w4_spouse_age=rowmean(age_person*)

		** Age gap
			gen w4_spouse_agegap=w4_age-w4_spouse_age

		** Birth year
			gen w4_birthyear=hh_b06b
			recode w4_birthyear 9999=. 9998=.
			replace w4_birthyear=2016-w4_age if missing(w4_birthyear)
			
	***********
	***********
	
	** Geographic information
	
		gen w4_urban=(urban==1) if !missing(urban)
		
	** Consumption
	
		*gen w4_conspc=expagg/adulteq
		gen w4_conspc=expagg/hhsize
			sum rexpaggpc, de
			
			/*gen temp=1 if w4_age<=12 & w4_inhh==1
			bys hhid: egen w4_hh_under14=total(temp)
			drop temp
			
			gen temp=1 if w4_female==1 & w4_age>12 & w4_inhh==1
			bys hhid: egen w4_hh_femadult=total(temp)
			drop temp
			
			gen temp=1 if w4_female==0 & w4_age>12 & w4_inhh==1
			bys hhid: egen w4_hh_maleadult=total(temp)
			drop temp
			
			gen equivalence=w4_hh_maleadult+w4_hh_femadult+w4_hh_under14
			
			scatter equivalence adulteq*/
			
			*scatter w4_conspc rexpaggpc if rexpaggpc<`r(p99)' // to check
		
	***********
	***********

	** LABOR

		** Worked for a non-HH member
			gen w4_work_outsideHH1=hh_e10>0 if !missing(hh_e10) // casual part-time
			gen w4_work_outsideHH2=hh_e11>0 if !missing(hh_e11) // wage or salary labor
		
			egen w4_work_outsideHH=rowmax(w4_work_outsideHH1 w4_work_outsideHH2)
			
			gen w4_work_outsideHH1_hrs=hh_e10 if !missing(hh_e10) // casual part-time
			gen w4_work_outsideHH2_hrs=hh_e11 if !missing(hh_e11) // wage or salary labor
			
			egen w4_work_outsideHH_hrs=rowtotal(w4_work_outsideHH1_hrs w4_work_outsideHH2_hrs), mi 
			replace w4_work_outsideHH_hrs=0 if w4_work_outsideHH==0
			
		** Worked in agriculture for a HH member
			gen w4_work_agHH1=hh_e07a>0 if !missing(hh_e07a) // farming
			gen w4_work_agHH2=hh_e07b>0 if !missing(hh_e07b) // livestock
			gen w4_work_agHH3=hh_e07c>0 if !missing(hh_e07c) // fishing
			
			egen w4_work_agHH=rowmax(w4_work_agHH1 w4_work_agHH2 w4_work_agHH3)
			
			gen w4_work_agHH1_hrs=hh_e07a if !missing(hh_e07a) // farming
			gen w4_work_agHH2_hrs=hh_e07b if !missing(hh_e07b) // livestock
			gen w4_work_agHH3_hrs=hh_e07c if !missing(hh_e07c) // fishing		
			
			egen w4_work_agHH_hrs=rowtotal(w4_work_agHH1_hrs w4_work_agHH2_hrs w4_work_agHH3_hrs), mi
			replace w4_work_agHH_hrs=0 if w4_work_agHH==0
			
		** Worked in HH business
			gen w4_work_bizHH1=hh_e08>0 if !missing(hh_e08) // run biz
			gen w4_work_bizHH2=hh_e09>0 if !missing(hh_e09) // help in biz
			
			egen w4_work_bizHH=rowmax(w4_work_bizHH1 w4_work_bizHH2)
			
			gen w4_work_bizHH1_hrs=hh_e08 if !missing(hh_e08) // run biz
			gen w4_work_bizHH2_hrs=hh_e09 if !missing(hh_e09) // help in biz
					
			egen w4_work_bizHH_hrs=rowtotal(w4_work_bizHH1_hrs w4_work_bizHH1_hrs), mi
			replace w4_work_bizHH_hrs=0 if w4_work_bizHH==0
			
		** Any work in last 7 days (agriculture, for non-HH member, own account/business)
			egen w4_worked_last7days=rowmax(w4_work_outsideHH w4_work_agHH w4_work_bizHH)
			
		** Hours worked in last 7 days
			egen w4_work_hrs=rowtotal(w4_work_outsideHH_hrs w4_work_agHH_hrs w4_work_bizHH_hrs)
			replace w4_work_hrs=. if missing(w4_work_outsideHH_hrs) & missing(w4_work_agHH_hrs) & missing(w4_work_bizHH_hrs)
				
		** Works in agriculture
			gen w4_work_agri=(w4_work_agHH==1) if !missing(w4_work_agHH)
		
	***********
	***********

	** HEALTH
		
		** Any illness or injury during the last 2 weeks
			gen w4_ill_inj=(hh_d04==1) if !missing(hh_d04)

		** Had to stop usual activity in the past 4 weeks
			gen w4_ill_inj_stopactivity=(hh_d08>0) if !missing(hh_d08)
			replace w4_ill_inj_stopactivity=0 if w4_ill_inj==0
			
	***********
	***********
	
	** SUBJECTIVE WELL-BEING
	
		** Access to health care
			gen w4_healthcare_inadequate=hh_t04==1 if !missing(hh_t04)
			
		** Food consumption
			gen w4_foodcons_inadequate=hh_t01==1 if !missing(hh_t01)
		
	***********
	***********
	
	** SAVE
	
		keep hhid indiv w4*
		order hhid indiv w4*
		save "$/data/MLW_w4_individual.dta", replace
